package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.IndustryInfo;
import cc.rengu.redp.bizimpl.upmp.service.IndustryInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:商户行业类别信息管理")
@RestController
@RequestMapping("/admin/merParam/industryInfo")
public class IndustryInfoController extends BaseController {

    @Autowired
    private IndustryInfoService industryInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, IndustryInfo entity) {
        return getDataTable(this.industryInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public IndustryInfo getOne(@NotBlank(message = "{required}") String id) {
        return industryInfoService.getById(id);
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "industryType", value = "行业编号",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "industryName", value = "行业名称",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "industryDesc", value = "行业描述",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "grpNo", value = "商户组别编号",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "merBusiType", value = "商户经营类型", required = true, dataType = "String")
    })
    public void add(@Valid IndustryInfo entity) throws RedpException {
        try {
            this.industryInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid IndustryInfo entity) throws RedpException {
        try {
            this.industryInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.industryInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.industryInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "industryType", value = "行业代码",  dataType = "String")
    })
    public RedpResponse check(String id, String industryType) throws RedpException {
        LambdaQueryWrapper<IndustryInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(industryType)) {
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(IndustryInfo::getId, id);
            }
            wrapper.eq(IndustryInfo::getIndustryType, industryType);
            IndustryInfo info = industryInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 行业代码 ] 重复，请重新输入");
            }
        }
        return new RedpResponse().message("验证成功");
    }
}
